<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="90px" label-position="right"
            v-show="showSearch">
            <el-form-item label="退库单编码" prop="code">
                <el-input v-model.trim="queryParams.code" style="width: 200px;" placeholder="请输退库单编码"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="机台" prop="machineName">
                <el-input v-model.trim="queryParams.machineName" style="width: 200px;" placeholder="请输入机台"
                    @keyup.enter="handleQuery" />

            </el-form-item>
            <el-form-item label="退库时间" prop="returnDate">
                <el-date-picker clearable v-model="queryParams.returnDate" style="width: 200px;" type="date"
                    value-format="YYYY-MM-DD" placeholder="请选择退库时间">
                </el-date-picker>
            </el-form-item>

            <el-form-item label="部门" prop="deptId">
                <el-tree-select style="width: 200px" v-model="queryParams.deptId" :data="deptOptions"
                    :props="{ value: 'id', label: 'label', children: 'children' }" value-key="id" placeholder="请选择部门"
                    check-strictly />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>
        <div>
            <el-table v-loading="loading" border :data="dataList">
                <el-table-column label="序号" width="60" type="index" align="center" />
                <el-table-column label="状态" min-width="100" align="center" :show-overflow-tooltip="true" prop="status"
                    v-if="columns[0].visible">
                    <template v-slot="{ row }">
                        <dict-tag :options="mes_handle_state" :value="row.status" />
                    </template>
                </el-table-column>
                <el-table-column label="退库单编码" min-width="200" align="center" :show-overflow-tooltip="true" prop="code"
                    v-if="columns[1].visible">
                    <template v-slot="{ row }">
                        <el-button type="primary" @click="handleView(row)" link>{{ row.code
                        }}</el-button>
                    </template>
                </el-table-column>
                <el-table-column label="退库时间" align="center" min-width="150" :show-overflow-tooltip="true" prop="returnDate"
                    v-if="columns[2].visible">
                    <template #default="scope">
                        <span>{{ scope.row.returnDate}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="部门" min-width="120" align="center" :show-overflow-tooltip="true" prop="deptName"
                    v-if="columns[3].visible">
                </el-table-column>
                <el-table-column label="机台" min-width="120" align="center" :show-overflow-tooltip="true" prop="machineName"
                    v-if="columns[4].visible">
                </el-table-column>
                <el-table-column label="所属车间" min-width="120" align="center" :show-overflow-tooltip="true" prop="workshopName"
                    v-if="columns[5].visible">
                </el-table-column>
                <el-table-column label="制单人" min-width="120" align="center" :show-overflow-tooltip="true" prop="createBy"
                    v-if="columns[6].visible">
                </el-table-column>
                <el-table-column label="备注" min-width="120" align="center" :show-overflow-tooltip="true" prop="remark"
                    v-if="columns[7].visible">
                </el-table-column>
                <el-table-column label="创建人" min-width="120" align="center" :show-overflow-tooltip="true" prop="createBy"
                    v-if="columns[8].visible">
                </el-table-column>
                <el-table-column label="创建时间" min-width="120" align="center" :show-overflow-tooltip="true" prop="createTime"
                    v-if="columns[9].visible">
                </el-table-column>
                <el-table-column label="操作" align="center" width="150" fixed="right" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-tooltip content="修改" placement="top" v-if="scope.row.status == 0">
                            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
                        </el-tooltip>
                        <el-tooltip content="删除" placement="top" v-if="scope.row.status == 0">
                            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
                        </el-tooltip>
                        <el-button link type="primary" icon="Printer" v-if="scope.row.status == 0"
                            @click="handleSubmit(scope.row)">提交</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList" />
        </div>
    </div>
</template>
<script setup name="returnSlip">
import { downLoadBlob } from '@/utils/ruoyi'
import useTagsViewStore from '@/store/modules/tagsView'
const tagsViewStore = useTagsViewStore()
import { listMaterialreturn, updateMaterialreturn, delMaterialreturn } from '@/api/storage/returnSlip.js'
import { deptTreeSelect } from "@/api/system/user";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([])
const showSearch = ref(true);
const total = ref(0);
const router = useRouter()
const route = useRoute()
const deptOptions = ref([])
// 列显隐信息
const columns = ref([
    { key: 0, label: `状态`, visible: true },
    { key: 1, label: `退库单编码`, visible: true },
    { key: 2, label: `退库时间`, visible: true },
    { key: 3, label: `部门`, visible: true },
    { key: 4, label: `机台`, visible: true },
    { key: 5, label: `所属车间`, visible: true },
    { key: 6, label: `制单人`, visible: true },
    { key: 7, label: `备注`, visible: true },
    { key: 8, label: `创建人`, visible: true },
    { key: 9, label: `创建时间`, visible: true },
]);

const data = reactive({
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehousingCode: undefined,
        supplierName: undefined,
        deliveryMan: undefined,
        warehousingDate: undefined,
    },
});
const { queryParams } = toRefs(data);
const { mes_handle_state,
} = proxy.useDict("mes_handle_state");

function getDeptTree() {
    deptTreeSelect().then(response => {
        deptOptions.value = response.data;
    });
};
if (route.query.code) {
    queryParams.value.code = route.query.code
    listMaterialreturn(queryParams.value).then(res => {
        if (res.rows.length > 0) {
            tagsViewStore.delView(route)
            setTimeout(() => {
                handleView(res.rows[0])
            }, 0)
        }
    })
}

const getList = () => {
    loading.value = true
    listMaterialreturn(queryParams.value).then(res => {
        loading.value = false
        dataList.value = res.rows
        total.value = res.total
    }).catch(err => {
        loading.value = false
    })
}
if (route.query?.inCode) {
    queryParams.value.inCode = route.query.inCode
    listMaterialin(queryParams.value).then(res => {
        if (res.rows.length > 0) {
            tagsViewStore.delView(route)
            setTimeout(() => {
                handleView(res.rows[0])
            }, 0)
        }
    })
} else {
    getList();
}
// 新增
const handleAdd = () => {
    router.push('/material/returnSlip/details')
}
// 编辑 
const handleUpdate = (row) => {
    router.push({
        path: '/material/returnSlip/details',
        query: {
            id: row.id,
            code: row.code
        }
    })
}
// 查看
const handleView = (row) => {
    router.push({
        path: '/material/returnSlip/details',
        query: {
            id: row.id,
            readonly: true,
            code: row.code
        }
    })
}
// 搜索
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef");
    handleQuery();
};
/** 删除按钮操作 */
function handleDelete(row) {
    proxy.$modal.confirm('是否确认删除"' + row.code + '"的物料退库单？').then(function () {
        return delMaterialreturn(row.id);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => { });
};
// 提交
const handleSubmit = (row) => {
    proxy.$modal.confirm('是否确认提交"' + row.code + '"的物料退库单？').then(function () {
        const formData = { ...row }
        formData.status = 1
        return updateMaterialreturn(formData);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("提交成功");
    }).catch(() => { });
}
// 导出
const handleExport = () => {
    materialinExport(queryParams.value).then(res => {
        downLoadBlob(res, '物料退库单列表')
    })
}

// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
getDeptTree()
getList()
</script>

<style lang="scss" scoped></style>